<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/8/24
 * Time: 17:31
 */

namespace Biz\module\Classes;

use Biz\model\trade\OrderPackage as mOrderPackage;
use Biz\service\SkuInfo as sSkuInfo;
use Biz\service\trade\Logistics as sLogistics;
use Biz\service\trade\ShopCourier as sShopCourier;
use Biz\model\trade\ShopCourier as mShopCourier;
use Biz\model\theParagraph\Store;


class OrderPackage
{


    //取指定订单号的所有包裹列表
    public static function getOrderPackageList($orderNo, $withSkuInfo = false, $withExpress = false, $uid = 0)
    {
        $list = [];
        $skuIdAll = [];
        $where = ['order_no' => $orderNo,'ORDER'=>['own_id'=>'ASC']];
        if ($uid) {
            $where['uid'] = $uid;
        }
        $packages = mOrderPackage::select('*', $where);

        if (empty($packages)) {
            return [];
        }

        //SKUlist
        $skuIdList = array_map(function ($v) {
            return array_filter(explode(',', $v));
        }, array_column($packages, 'sku_id_list', 'id'));

        array_map(function ($v) use (&$skuIdAll) {
            $skuIdAll = array_merge($skuIdAll, $v);
        }, $skuIdList);

        if (!empty($skuIdAll) && $withSkuInfo) {
            $skuInfoList = sSkuInfo::getSkusInfoByIds($skuIdAll);
        }


        $list = array_map(function ($v) use ($skuIdList, &$skuInfoList, $withSkuInfo, $withExpress) {

            $skuList = [];
            if ($withSkuInfo) {
                $skuList = array_map(function ($v) use (&$skuInfoList) {
                    $skuInfo = $skuInfoList[$v];

                    return [
                        'sku_id'          => $skuInfo['id'],
                        'thumb_cover_img' => $skuInfo['extend_info_thumb_url'] ?  : $skuInfo['goods_info']['spec_img_thumb_url']
                    ];

                }, $skuIdList[$v['id']]);
            }else{
                $skuList = $skuIdList[$v['id']];
            }

            $expressInfo = [];

            if ($withExpress) {
                try {
                    $expressInfo = (array)sLogistics::trackingmore($v['order_no'], $v['id']);
                } catch (\Exception $e) {
                }
            }

            $courierId = sShopCourier::getCourierIdByCourierName($v['express_company']);

            $data = [
                'package_id'      => $v['id'],
                'package_title'   => $v['own_id'] > 0 ? '包裹' . $v['own_id'] : '',
                'courier_id'      => $courierId,
                'express_company' => $v['express_company'],
                'express_no'      => $v['express_no'],
                'delivery_time'      => $v['delivery_time'],
                'sku_list'        => $skuList,
            ];


            if($data['courier_id'] == mShopCourier::SELF_LIFTING_ID){
                $paragraphInfo = Store::getStoreById($data['express_no']);
                $data['express_address'] = isset($paragraphInfo['location'])?$paragraphInfo['location']:'';
            }

            if(!empty($expressInfo)){
                $data = array_merge($data, $expressInfo);
            }

            return $data;
        }, $packages);


        return $list;

    }



}